import os

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from ui_mainwindow import *
from visualizer import *
    
class MyMainWindow(QtGui.QMainWindow):
    
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        #Additional Singals
        QtCore.QObject.connect(self.ui.actionSelect_Dir, QtCore.SIGNAL("triggered()"), self.on_actionSelect_Dir_triggered)
        #QtCore.QObject.connect(self.ui.actionLoad_Workspace, QtCore.SIGNAL("triggered()"), self.on_actionLoad_Workspace_triggered)
        #Additional Variables
        self.basedir = ""
        self.workspace = os.environ["TEMP"]
    
    @QtCore.pyqtSlot()
    def on_pushButtonRefresh_clicked(self):
        #TODO: Reload the file list
        self.hurricane_list = sorted(os.listdir(self.basedir))
        self.ui.treeWidgetFolder.clear()
        self.ui.listWidgetElevation.clear()
        self.ui.listWidgetTime.clear()
        for i in range(len(self.hurricane_list)):         
            wi = QtGui.QTreeWidgetItem(self.ui.treeWidgetFolder)
            wi.setText(0, self.hurricane_list[i])
            self.ui.treeWidgetFolder.addTopLevelItem(wi)
        pass   
        
    @QtCore.pyqtSlot()
    def on_pushButtonDraw_clicked(self):
        hurricane_name = str(self.ui.treeWidgetFolder.currentItem().text(0))
        height = str(self.ui.listWidgetElevation.currentItem().text())
        date_time = str(self.ui.listWidgetTime.currentItem().text())
        dataset = os.path.join(str(self.basedir), \
                               hurricane_name, \
                               'merge', 'MergedReflectivityQC', \
                               height, \
                               date_time)
        data_map(dataset, None, hurricane_name, height, date_time).draw()
        pass
    
    @QtCore.pyqtSlot()
    def on_actionSelect_Dir_triggered(self):
        self.basedir = QtGui.QFileDialog.getExistingDirectory(self, "Hurricane Folder", "W:\\matyas\\Hurricane_Data")
        print self.basedir
        
    @QtCore.pyqtSlot()    
    def on_actionLoad_Workspace_triggered(self):
        self.workspace = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Workspace", self.workspace))
        os.environ["TEMP"] = self.workspace
        self.statusBar().showMessage("Current Workspace is %s" % self.workspace)
        pass
        
    def on_treeWidgetFolder_itemClicked(self, item, column):
        try:          
            self.elevations = sorted(os.listdir(os.path.join(str(self.basedir), str(item.text(0)), 'merge', 'MergedReflectivityQC')))
            print self.elevations
        except:
            QMessageBox.critical(self, "Path Error!", "This folder is not a valid hurricane archive")
            return
        #Add to elevations
        c = self.ui.listWidgetElevation
        c.clear()
        c.addItems(QtCore.QStringList(self.elevations))
        #TODO: Automatically select first
        c.setCurrentRow(0)
        c.itemClicked.emit(c.item(0))
        #self.on_listWidgetElevation_itemClicked(c.item(0))
        
    
    def on_listWidgetElevation_itemClicked(self, item):
        #Only list folder once.
        c = self.ui.listWidgetTime
        self.times = sorted(os.listdir(os.path.join(str(self.basedir), str(self.ui.treeWidgetFolder.currentItem().text(0)), 'merge', 'MergedReflectivityQC', str(item.text()))))
        print self.times
        c.clear()
        c.addItems(QStringList(self.times))
    